/*******************************************************************************
 * Venice Noise Android Application
 * Copyright (C) 2011  Worcester Polytechnic Institute, Wesley Ripley
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * If you plan to use or modify any of this code please contact Fabio Carrera (carrera.fabio@gmail.com)
 * Also please attribute any code used in the final product to the developers. 
 * 
 * Author: Wesley Ripley (wripley@wpi.edu) 
 *******************************************************************************/
package org.venicenoise.app.location;

import android.location.Location;

/**
 * A LocationPicker for picking locations that are a better version
 * of the same physical location
 * 
 * @author Wesley Ripley
 * @version 12/16/2011
 *
 */
public class SameLocationPicker implements LocationPicker {

	private final LocationPicker normalLocationPicker;
	
	/**
	 * Constructor for a SameLocationPicker
	 * @param normalLocationPicker
	 * 		The location picker that would be used to pick the location
	 * 		if we didn't want to keep the same physical location
	 */
	public SameLocationPicker(LocationPicker normalLocationPicker)
	{
		this.normalLocationPicker = normalLocationPicker;
	}
	
	/**
	 * Constructor for a SameLocationPicker.
	 */
	public SameLocationPicker()
	{
		this(new TagEstimateLocationPicker());
	}
	
	public boolean isBetterLocation(Location location, Location currentLocation) {
		//Null location is never better
		if(location == null)
		{
			return false;
		}
		//if the new location is not an all around better location
		//it cannot be a better version of the same location
		if(!normalLocationPicker.isBetterLocation(location,currentLocation))
		{
			return false;
		}
		float taggedAccuracy = currentLocation.getAccuracy();
		//if the new location is less accurate then the tagged one
		//then it is not better
		if(location.getAccuracy() >= taggedAccuracy)
		{
			return false;
		}
		float distanceBetween = currentLocation.distanceTo(location);
		//if the distance between the points is not within the 
		//accuracy radius of the tagged location that the user
		//has moved and this is a new location, so we don't want it
		if(distanceBetween > taggedAccuracy)
		{
			return false;
		}
		//this means new location is more accurate and within the accuracy
		//radius of the tagged location so it is a better fix on the
		//same location
		return true;
	}

}
